package com.moke.module
{
	import com.moke.loader.ILoadingBar;
	
	import org.taomee.ds.HashMap;
	import org.taomee.utils.InvalidateCallback;
	

	/**
	 *
	 * 模块管理类
	 * @author fraser
	 */
	public class ModuleManager
	{
		private static var _moduleProxyMap:HashMap = new HashMap();

		public static function hasModuleShow():Boolean
		{
			var result:Boolean = false ;
			var modules:Array = _moduleProxyMap.getValues() ;
			for each(var moduleProxy:ModuleProxy in modules)
			{
				if (moduleProxy.status == ModuleStatus.SHOW)
				{
					result = true ; break;
				}
			}
			return result ;
		}
		
		/** 显示模块 */
		public static function showModule(url:String,title:String,initData:Object=null,loadingBar:ILoadingBar = null):void
		{
			var moduleProxy:ModuleProxy = _moduleProxyMap.getValue(url) as ModuleProxy;
			if (moduleProxy != null)
			{
				moduleProxy.init(initData);
				moduleProxy.show();
			}
			else
			{
				createModule(url,title,initData,loadingBar);
			}
		}

		public static function toggleModule(url:String,title:String,initData:Object=null,loadingBar:ILoadingBar = null):void
		{
			var moduleProxy:ModuleProxy = _moduleProxyMap.getValue(url) as ModuleProxy;
			if (moduleProxy != null)
			{
				if (moduleProxy.status == ModuleStatus.SHOW)
				{
					moduleProxy.hide();
				}
				else
				{
					moduleProxy.init(initData);
					moduleProxy.show();
				}
			}
			else
			{
				createModule(url,title,initData,loadingBar);
			}
		}

		/** 关闭当前所有模块 */
		public static function closeAllModule():void
		{
			var modules:Array = _moduleProxyMap.getValues() ;
			for each(var moduleProxy:ModuleProxy in modules)
			{
				if (moduleProxy.lifecycleType == ModuleLifecycleType.CURRENT)
				{
					InvalidateCallback.addFunc(moduleProxy.dispose);
				}
				else
				{
					InvalidateCallback.addFunc(moduleProxy.hide);
				}
			}
		}

		/** 创建新的模块代理 */
		private static function createModule(url:String,title:String,initData:Object,loadingBar:ILoadingBar):void
		{
			var moduleProxy:ModuleProxy = new ModuleProxy(url,title,loadingBar);
			moduleProxy.init(initData);
			moduleProxy.show();
			if (_moduleProxyMap.containsKey(url))
			{
				throw new Error("模块命名有冲突！！");
			}
			_moduleProxyMap.add(url,moduleProxy);
		}

		/** 获取模块代理信息 */
		public static function getModuleProxy(url:String):ModuleProxy
		{
			var moduleProxy:ModuleProxy = _moduleProxyMap.getValue(url);
			if (moduleProxy)
			{
				return moduleProxy;
			}
			return null;
		}

		public static function closeForInstance(instance:BaseModule):void
		{
			closeForURL(instance.url);
		}

		public static function closeForURL(url:String):void
		{
			var moduleProxy:ModuleProxy = _moduleProxyMap.getValue(url);
			if (moduleProxy)
			{
				if (moduleProxy.lifecycleType == ModuleLifecycleType.CURRENT)
				{
					_moduleProxyMap.remove(url);
					InvalidateCallback.addFunc(moduleProxy.dispose);
				}
				else
				{
					InvalidateCallback.addFunc(moduleProxy.hide);
				}
			}
		}
		
		public static function removeModuleProxy(url:String):void
		{
			var moduleProxy:ModuleProxy = _moduleProxyMap.getValue(url);
			if(moduleProxy)
			{
				_moduleProxyMap.remove(url) ;
			}
		}
	}
}
